package com.thuan.db;

import java.sql.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.thuan.hibernate.Repository;
import com.thuan.pojo.Competition;
import com.thuan.pojo.League;
import com.thuan.pojo.Reward;
import com.thuan.pojo.UserAccount;

public class LeagueDAO implements ILeagueDAO{

	private static SessionFactory sessionFactory;
	
	public static void setSessionFactory(SessionFactory sessionFactory) {
		LeagueDAO.sessionFactory = sessionFactory;
	}
	
	@Override
	public List<League> listLeagues() {
		Session session = sessionFactory.openSession();

		List<League> leagues = session.createQuery("from League").list();
		session.beginTransaction().commit();
		session.close();
		
		return leagues;
	}

	@Override
	public int addLeague(String name, Date startDate, Date endDate,
			int rewardId) {
		
		if(findLeague(name) != null)
			return -1;
		
		Reward reward = this.findReward(rewardId);
		League league = new League(name, reward, startDate, endDate);
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		Integer id = (Integer) session.save(league);
		
		transaction.commit();
		session.close();
		
		return id;
	}

	private Reward findReward(int rewardId) {
		Reward result = null;
		
		Session session = sessionFactory.openSession();
		
		Query query = session.createQuery("from Reward where reward_id = :rewardId");
		query.setParameter("rewardId", rewardId);
		List<Reward> rewards = query.list();
		
		if(!rewards.isEmpty())
			result = rewards.get(0);
		
		session.close();
		return result;
	}

	@Override
	public int addCompetition(int leagueId, String usernamePlayerA, String usernamePlayerB, String usernameWinner) {
		League league = this.findLeague(leagueId);
		UserAccount playerA = Repository.findUser(usernamePlayerA);
		UserAccount playerB = Repository.findUser(usernamePlayerB);
		UserAccount winner = Repository.findUser(usernameWinner);
		
		if(findCompetition(playerA, playerB) != null)
			return -1;
		
		Competition competition = new Competition(league, playerA, playerB, winner);
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		Integer id = (Integer) session.save(competition);
		
		transaction.commit();
		session.close();
		
		return id;
	}

	@Override
	public List<UserAccount> getWinners(League league) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public League findLeague(String name) {
		League result = null;
		
		Session session = sessionFactory.openSession();
		
		Query query = session.createQuery("from League where name = :name");
		query.setParameter("name", name);
		List<League> leagues = query.list();
		
		if(!leagues.isEmpty())
			result = leagues.get(0);
		
		session.close();
		return result;
	}
	
	private League findLeague(int leagueId) {
		League result = null;
		
		Session session = sessionFactory.openSession();
		
		Query query = session.createQuery("from League where league_id = :leagueId");
		query.setParameter("leagueId", leagueId);
		List<League> leagues = query.list();
		
		if(!leagues.isEmpty())
			result = leagues.get(0);
		
		session.close();
		return result;
	}

	@Override
	public Competition findCompetition(UserAccount playerA, UserAccount playerB) {
		Competition result = null;
		
		Session session = sessionFactory.openSession();
		
		Query query = session.createQuery("from Competition where playerA = :playerA and playerB = :playerB");
		query.setParameter("playerA", playerA);
		query.setParameter("playerB", playerB);
		
		List<Competition> competitions = query.list();
		
		if(!competitions.isEmpty())
			result = (Competition)competitions.get(0);
		
		session.close();
		return result;
	}

}
